package cn.exrick.xboot.modules.research.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;

import cn.exrick.xboot.modules.research.entity.PatentManagement;
import cn.exrick.xboot.modules.research.service.IPatentManagementService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lty
 */
@Slf4j
@RestController
@Api(tags = "科研专利管理管理接口")
@RequestMapping("/xboot/patentManagement")
@Transactional
public class PatentManagementController {

    @Autowired
    private IPatentManagementService iPatentManagementService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<PatentManagement> get(@PathVariable String id) {

        PatentManagement patentManagement = iPatentManagementService.getById(id);
        return new ResultUtil<PatentManagement>().setData(patentManagement);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<PatentManagement>> getAll() {

        List<PatentManagement> list = iPatentManagementService.list();
        return new ResultUtil<List<PatentManagement>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<PatentManagement>> getByPage(@ModelAttribute PatentManagement patentManagement,@ModelAttribute PageVo page) {
        QueryWrapper<PatentManagement> qw =new QueryWrapper<>();

        if (StrUtil.isNotBlank(patentManagement.getPatentId())){
             qw.like("patent_Id",patentManagement.getPatentId());
        }

        if (StrUtil.isNotBlank(patentManagement.getPatentName())){
            qw.like("patent_Name",patentManagement.getPatentName());
        }

        if (StrUtil.isNotBlank(patentManagement.getPatentee())){
            qw.like("patentee",patentManagement.getPatentee());
        }

        if (StrUtil.isNotBlank(patentManagement.getTime())){
            qw.eq("time",patentManagement.getTime());
        }

        IPage<PatentManagement> data = iPatentManagementService.page(PageUtil.initMpPage(page),qw);
        return new ResultUtil<IPage<PatentManagement>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<PatentManagement> saveOrUpdate(PatentManagement patentManagement) {

        if (iPatentManagementService.saveOrUpdate(patentManagement)) {
            return new ResultUtil<PatentManagement>().setData(patentManagement);
        }
        return new ResultUtil<PatentManagement>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@RequestParam String[] ids) {

        for (String id : ids) {
            iPatentManagementService.removeById(id);
        }
        return ResultUtil.success("批量通过id删除数据成功");
    }
}
